package com.chenfei.ioc;

import com.chenfei.ioc.bean.FirstBean;
import com.chenfei.ioc.di.ConstructorBean;
import com.chenfei.ioc.di.SetBean;
import com.chenfei.ioc.di.SortBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author chenfei
 * @description
 * @since 2018/3/22
 */
public class MainIOC {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-ioc.xml");
        fourTest(context);
    }

    /**
     * spring第一个例子，通过xml文件初始化bean
     * @param context
     */
    public static void firstTest(ApplicationContext context){
        FirstBean firstBean = context.getBean("firstBean",FirstBean.class);
        firstBean.myFirstMethod();
    }

    /**
     * 演示依赖注入（DI）的set方式注入
     * @param context
     */
    public static void secondTest(ApplicationContext context){
        SetBean setBean = context.getBean("setBean",SetBean.class);
        setBean.setMethod();
    }

    /**
     * 演示依赖注入（DI）的构造器方式注入
     * @param context
     */
    public static void thirdTest(ApplicationContext context){
        ConstructorBean constructorBean = context.getBean("constructorBean",ConstructorBean.class);
        constructorBean.constructorMethod();
    }

    /**
     * 演示依赖注入（DI）的两种注入方式的先后顺序
     * @param context
     */
    public static void fourTest(ApplicationContext context){
        SortBean sortBean = context.getBean("sortBean",SortBean.class);
    }

}
